import {
	objDeepCopy,
	getDateString
} from "@/assets/js/common.js";
import commonObject from "@/assets/js/xm.js";
let Controller = {
	created() {
		this.init();
	},
	mounted() {

	},
	methods: {
		init() {
			var that = this;
			this.getTableFuction(that);
			this.getMaterialOption(that, 'material'); //获取材质
			this.getKeyDetail(that, 'goldPrice') //获取24k金价
		},
		selectedHighlight(data) {
			// 如果选中则添加高亮样式
			if (this.Model.TableSelectData.indexOf(data.row) > -1) {
				return {
					"background-color": "#FEF6EE"
				};
				return {}
			}
		},
		//列表
		getTableFuction(that, pramas) {
			var apiTest = this.apiTest;
			pramas = pramas || {};
			this.$ajax({
				method: 'post',
				// url:'/supplier/supplierGoldExchange/list',
				url: apiTest + '/supplier/supplierGoldExchange/list',
				data: pramas
			}).then((res) => {
				that.Model.tableData = []; //清空数组数据
				var data = res.data;
				if (data.status == 200) {
					if (data.result) {
						var pagination = commonObject.paginationFunction(data.result);
						that.Model.paginationObject = objDeepCopy(pagination);
						var result = data.result.result;
						if (result.length) {
							var trObj = {};
							for (var i = 0; i < result.length; i++) {
								trObj = objDeepCopy(result[i]);
								trObj.showMaterial = result[i].materialDictionary.value;
								that.Model.tableData.push(trObj);
							}
						}
					}
				} else if (data.status == 500) {
					that.tips(that, 'warning', data.msg);
				}

			});
		},
		//*****************************************************获取下拉选项
		getMaterialOption(that, type) {
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'post',
				url: apiTest + '/base/systemDictionary/getByType/' + type,
			}).then((res) => {
				var materialData = [];
				var data = res.data;
				if (data) {
					var result = data.result;
					for (var i = 0; i < result.length; i++) {
						materialData.push({
							label: result[i].value,
							value: result[i].key
						});
					}
					that.Model.filterOption.items[0].data = objDeepCopy(materialData);
				}
			});
		},
		//*****************************************************根据key获取详情(获取24k金价)
		getKeyDetail(that, key) {
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'get',
				url: apiTest + '/base/systemContent/getByKey/' + key,
			}).then((res) => {
				// console.log(res.data);
				that.Model.popForm.goldPrice = res.data.result.value;
			});
		},
		//*******************************************************获取 批量新增列表
		getAddList(that, type) {
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'post',
				url: apiTest + '/base/systemDictionary/getByType/' + type,
				data: {}
			}).then((res) => {
				console.log(res);
				var result = res.data.result;
				var obj = {}
				if (result) {
					that.Model.popForm.addListData = [];
					for (var i = 0; i < result.length; i++) {
						obj = {
							label: result[i].value,
							key: result[i].key,
							proportion: 0.755,
						}
						obj.goldPrice2 = that.Model.popForm.goldPrice * obj.proportion;
						that.Model.popForm.addListData.push(obj);
					}
				}

			});
		},
		//*********************************************************批量新增
		addConversionFunction(that, pramas) {
			var apiTest = this.apiTest;
			pramas = pramas || {};
			this.$ajax({
				method: 'post',
				url: apiTest + '/supplier/supplierGoldExchange',
				data: pramas
			}).then((res) => {
				var status = res.data.status;
				if (status == 200) {
					that.tips(that, 'success', '新增成功');
					//成功刷新列表
					that.getTableFuction(that, that.searchObject);
					//
					// that.filterOption.length = 0;
				} else {
					that.tips(that, 'warning', res.data.msg);
				}
			});
		},
		handleCurrentChange(val) { //******************************************点击分页
			// console.log(`当前页: ${val}`);
			var that = this;
			this.Model.currentPage = val;
			var pramas = this.Model.searchObject;
			pramas.pageIndex = val;
			this.getTableFuction(that, pramas)
		},
		showdialog() { //点击新建
			this.Model.dialogTableNew = true;
			this.getAddList(this, 'material')
		},
		handleSizeChange(val) {
			console.log(`每页 ${val} 条`);
			var pramas = this.Model.searchObject;
			pramas.pageSize = val;
			this.getTableFuction(this, pramas);
		},
		handleSelectionChange(data) {
			this.Model.TableSelectData = data;
		},
		//点击保存 换算
		offerInfoAddSave() {
			var obj = {}
			var pramas = [];
			var listData = this.Model.popForm.addListData;
			if(this.Model.popForm.goldPrice < 0){
				this.tips(this, 'warning', '金价不能为负值');
				return;
			}
			//
			for (var i = 0; i < listData.length; i++) {
				if(listData[i].proportion < 0){
					this.tips(this, 'warning', '比例不能为负值');
					return;
				}else{
					obj = {
						material: listData[i].key,
						twentyFourGoldPrice: Number(this.Model.popForm.goldPrice),
						proportion: Number(listData[i].proportion),
						goldPrice: Number(this.Model.popForm.goldPrice * listData[i].proportion)
					}
					pramas.push(obj)
				}
			}
			this.addConversionFunction(this, pramas);
		},
		selectedHighlight(data) {
			// 如果选中则添加高亮样式
			if (this.Model.TableSelectData.indexOf(data.row) > -1) {
				return {
					"background-color": "#FEF6EE"
				};
				return {}
			}
		},
		// 
		showChild(val) {
			var startTime;
			var endTime;
			if (val.createDate) {
				startTime = getDateString(val.createDate[0])
				endTime = getDateString(val.createDate[1])
			} else {
				startTime = undefined;
				endTime = undefined;
			}
			var obj = {
				condition: {
					material: val.material,
					startTime: startTime,
					endTime: endTime
				},
				pageIndex: 1,
				pageSize: 10

			}
			this.Model.searchObject = obj;
		},
		toSearch(val) { //查询
			var pramas = this.Model.searchObject;
			console.log('pramas', pramas);
			this.getTableFuction(this, pramas)

		},
		toClear(val) {
			
		},
		toExport(val) {
			console.log(val);
		},
		tips(that, type, message) {
			let duration;
			type == 'success' ? duration = 3000 : duration = 10000;
			that.$message({
				message: message,
				type: type,
				duration: duration
			});
		},
	},
	watch: {
		$route: {
			handler(newValue, oldValue) {

			},
			deep: true
		},

	},
	filters: {
		objFilters: function(value) {

		}
	}
}
export default Controller;
